<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;

/**
 * Class PayerSummary
 * @package app\home\controller
 * 应付汇总统计'
 */
class PayerSummary extends BaseController
{
    protected $table='sale_reconcile';
    protected $title='应付汇总统计';
    private $sessionKey='pagePayerSummary';
    public function index()
    {
        $param=$this->param;
        if (request()->isAjax()) {
            $where[]=['sr.status','=',2];
            $where[]=['sr.s_type','=',1];
            foreach (['p_name'] as $i){
                if (isset($param[$i])&&$param[$i]){
                    $k=str_replace('_','.',$i);
                    $where[]=[$k,'like','%'.$param[$i].'%'];
                }
            }
            if (isset($param['orderDate'])&&$param['orderDate']){
                $times=explode('~',$param['orderDate']);
                $where[]=['sr.reconcileDate','between',$times];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            session($this->sessionKey,$where);
            $list = Db::name($this->table)
                ->alias('sr')
                ->join('purveyor p','p.id=sr.punter_id','left')
                ->where($where)
                ->group('sr.punter_id')
                ->field("sr.*,p.name as pName,
                (select sum(money) from fy_receipt_order_detail rod where rod.status=2 and rod.punter_id=sr.punter_id and rod_type=1)savedMoney")
                ->paginate($rows)->each(function ($item,$index){
                    $ids[]=$item['id'];
                    $item['savedMoney']=$item['savedMoney']??0;
                    $item['reconcilePrice']=Db::name('sale_reconcile')
                        ->where('status','>',1)->where('punter_id',$item['punter_id'])
                        ->sum('reconcilePrice');
                    $item['unPrice']=$item['reconcilePrice']-$item['savedMoney'];
                    return $item;
                })->toArray();
            $ids=Db::name($this->table)
                ->where($where)
                ->alias('sr')
                ->join('purveyor p','p.id=sr.punter_id','left')->column('sr.id');
            $yingshou=Db::name($this->table)->where('id','in',$ids)->sum('reconcilePrice');
            $yishou=Db::name('receipt_order_detail')->where('status',2)
                ->where('sale_reconcile_id','in',$ids)->sum('money');
            $weihui=$yingshou-$yishou;
            $data=['code'=>0,'msg'=>'获取成功','count'=>$list['total'],'data'=>$list['data'],'ys'=>$yingshou,'yhk'=>$yishou,'whk'=>$weihui];
            return $data;
        } else {

            return view();
        }

    }

    public function exportData()
    {
        $where=session($this->sessionKey);
        $list = Db::name($this->table)
            ->alias('sr')
            ->join('purveyor p','p.id=sr.punter_id','left')
            ->where($where)
            ->group('sr.punter_id')
            ->field("sr.*,p.name as pName,
                (select sum(money) from fy_receipt_order_detail rod where rod.status=2 and rod.punter_id=sr.punter_id and rod_type=1)savedMoney")
            ->select()->each(function ($item,$index){
                $ids[]=$item['id'];
                $item['savedMoney']=$item['savedMoney']??0;
                $item['reconcilePrice']=Db::name('sale_reconcile')
                    ->where('status','>',1)->where('punter_id',$item['punter_id'])
                    ->sum('reconcilePrice');
                $item['unPrice']=$item['reconcilePrice']-$item['savedMoney'];
                return $item;
            });
        halt();
        $title = [
//            'id' => 'ID',
            'pName' => '供应商',
            'reconcilePrice' => '应付金额',
            'savedMoney' => '已付款金额',
            'unPrice' => '未付款金额',

        ];
        $name =$this->title;
        exportXlsData($name,$title,$list);
        exit();
    }
}